/* 
 * Memories Version 1.0
 * Developed by Jigar Desai (http://www.jigar.net/) 
 */

using System;
using Memories.Entities;
using com.drew.imaging.jpg;

namespace Memories
{
	public class ExifPropertyPlugin: IPropertyPlugin
	{
		public ExifPropertyPlugin(){}
		
		
		public File ExtractValues(File file,System.IO.FileInfo fileInfo,Settings settings)
		{
			
			if(fileInfo.Extension == ".jpg" || fileInfo.Extension == ".png")
			{	
				int ExifTitle = 40091;
				int ExifComment = 40092;

				try 
				{
					com.drew.metadata.Metadata metadata = JpegMetadataReader.ReadMetadata(fileInfo);
					if(metadata.ContainsDirectory(typeof(com.drew.metadata.exif.ExifDirectory)))
					{
						com.drew.metadata.exif.ExifDirectory directory = (com.drew.metadata.exif.ExifDirectory)metadata.GetDirectory(typeof(com.drew.metadata.exif.ExifDirectory));
						if(directory.ContainsTag(ExifTitle))
						{
							file.Title = directory.GetDescription(ExifTitle);
						}
						if(directory.ContainsTag(ExifComment))
						{
							file.Comment = directory.GetDescription(ExifComment);
						}
					}
								
				} 
				catch (JpegProcessingException e) 
				{
								
				}
			}
			
			return file;

		}
	}
}